﻿package com.shuqi.sdk.ad;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 自主广告的网页显示Activity
 * 
 * @author Arthur Jin
 */
public class AdActivity extends Activity {
	private LinearLayout layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		String url = getIntent().getStringExtra("url");
		final WebView webView = new WebView(this);
		layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		LinearLayout navLayout = new LinearLayout(this);
		navLayout.setBackgroundColor(Color.BLACK);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 1);
		Button goBack = new Button(this);
		goBack.setText("向 后");
		goBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				webView.goBack();
			}
		});
		navLayout.addView(goBack, params);
		Button goForward = new Button(this);
		goForward.setText("向 前");
		goForward.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				webView.goForward();
			}
		});
		navLayout.addView(goForward, params);
		Button refresh = new Button(this);
		refresh.setText("刷 新");
		refresh.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				webView.reload();
			}
		});
		navLayout.addView(refresh, params);
		Button close = new Button(this);
		close.setText("关 闭");
		close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Animation anim = new TranslateAnimation(0, 0, 0, getWindowManager()
						.getDefaultDisplay().getHeight());
				anim.setDuration(1000);
				layout.startAnimation(anim);
				Runnable r = new Runnable() {
					@Override
					public void run() {
						finish();
					}
				};
				arg0.postDelayed(r, 1000);
			}
		});
		navLayout.addView(close, params);

		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				webView.loadUrl(url);
				return true;
			}
		});
		webView.loadUrl(url);
		layout.addView(webView, params);
		layout.addView(navLayout, ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		setContentView(layout);
		Animation anim = new TranslateAnimation(0, 0, getWindowManager().getDefaultDisplay()
				.getHeight(), 0);
		anim.setDuration(1000);
		layout.startAnimation(anim);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		try {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				Animation anim = new TranslateAnimation(0, 0, 0, getWindowManager()
						.getDefaultDisplay().getHeight());
				anim.setDuration(1000);
				layout.startAnimation(anim);
				Runnable r = new Runnable() {
					@Override
					public void run() {
						finish();
					}
				};
				layout.postDelayed(r, 1000);
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.onKeyDown(keyCode, event);
	}
}
